// pages/docreg/docreg.js
var app=getApp();
var host=app.globalData.host
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		name:'',
		hspt:'请选择',
		keshi:'请选择',
		phone:'',
		offices:[],
		hspts:["上海市罗店医院"]
	},
	bindhsptChange: function(e)
    {
		//医院复选框确认
		console.log(e);
        this.setData({
			indexh:e.detail.value,
		})
        this.setData({
            hspt: this.data.hspts[this.data.indexh]
		})
		this.getKeshiList()
    },
	bindkeshiChange: function(e)
    {
		//科室复选框确认
		console.log(e);
        this.setData({
			index:e.detail.value,
		})
        this.setData({
            keshi: this.data.offices[this.data.index]
        })
    },
	confirmName:function(e)
	{
		this.setData({
			name:e.detail.value
		})
	},
	confirmPhonenum:function(e)
	{
		this.setData({
			phone:e.detail.value
		})
	},
	confirmhspt:function(e)
	{
		this.setData({
			hspt:e.detail.value
		})
	},
	confirmkeshi:function(e)
	{
		this.setData({
			keshi:e.detail.value
		})
	},
	login()
	{
		wx.login({
			timeout: 1000,
			success (res)
			{
				console.log(res)
				if(res.code)
				{
					console.log(res.code);
					wx.request({
						method:'GET',
						url:host+'/login',
						data:{
							"code": res.code
						},
						success :ress=>
						{
							console.log(ress);
							if(ress.data.code==2)
							{
								console.log("未注册用户");
								wx.showToast({
								title: '未注册用户',
								icon:'error'
								})
								getApp().globalData.regingid=ress.data.data;
								wx.navigateTo({
									url: '../reg',
								})
							}
							else if(ress.data.code==0||ress.data.code==3)
							{
								getApp().globalData.isAdmin=true;
								getApp().globalData.userInfo_doc=ress.data;
								wx.switchTab({
									url: '../../../home/home',
								})
								wx.showToast({
									title: '欢迎',
								})
							}
							else
							{
								getApp().globalData.userInfo_doc=ress.data;
								wx.navigateBack({
								delta: 1,
								})
								wx.reLaunch({
								url: '../../../pmenu/pmenu',
								})
								wx.showToast({
									title: '欢迎',
								})
							}
						},
						fail:res=>
						{
							console.log(res);
							wx.showToast({
								title: '登录失败',
							})
						}
					})
				}
			}	,
			fail(res)
			{
				wx.showToast({
					title: '未得到授权',
				})
			}
		})
	},
	reg:function()
	{
		if(this.data.hspt==''||this.data.name==''||this.data.keshi=='请选择'||this.data.phone=='')
		{
			wx.showToast({
			  title: '请填写完整',
			  icon:'error'
			})
			return;
		}
		console.log({
			doctorHspt:this.data.hspt,
			doctorName:this.data.name,
			doctorOffice:this.data.keshi,
			doctorOpenid:app.globalData.regingid,
			doctorPhone:this.data.phone
		  }	)
		let that=this
		wx.request
		({
			url: host+'/register/doctor',
			method:'POST',
		  	data:{
				"doctorHspt": this.data.hspt,
 				"doctorId": 0,
				"doctorName": this.data.name,
				"doctorOffice": this.data.keshi,
				"doctorOpenid": app.globalData.regingid,
				"doctorPhone": this.data.phone
			},
			success:res=>
			{
				console.log(res);
				if(res.data.code==10000)
				{
					getApp().globalData.user=res.data;
					that.login();
				}
				else
				{
					wx.showToast({
						title: res.data.msg,
						icon:'error'
					})
				}
			},
			fail(res)
			{
				console.log(res)
				wx.showToast({
				  	title: res.msg
				})
			}
		})
	},
	getHsptList()
	{
		let that=this;
		wx.request({
			url: host+'/admin/hosptl',
			method:"GET",
			success(res)
			{
				console.log(res)
				var hsptl=res.data.data;
				var l=[];
				for(var i=0;i<hsptl.length;i++)
				{
					l[i]=hsptl[i].hosptlName;
				}
				that.setData({
					hsptList:hsptl,
					hspts:l
				})
			}
		})
	},
	getKeshiList()
	{
		var hsptl=this.data.hsptList;
		var hsptn=this.data.hspt;
		var idx=0;
		for(var i=0;i<hsptl.length;i++)
		{
			if(hsptl[i].hosptlName==hsptn)
			{
				idx=hsptl[i].hosptlId;
				break;
			}
		}
		console.log(idx)
		let that=this;
		wx.request({
			url: host+'/admin/office',
			method:"GET",
			data:{
				"id":idx
			},
			success(res)
			{
				console.log(res)
				var olist=res.data.data;
				var l=[]
				for(var i=0;i<olist.length;i++)
				{
					l[i]=olist[i].officeName;
				}
				that.setData({
					offices:l
				})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getHsptList()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})